package com.shouxin.attendance.database.dao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.shouxin.attendance.database.model.BabyCustody;

import com.shouxin.attendance.database.model.Custody;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "t_custody".
*/
public class CustodyDao extends AbstractDao<Custody, Long> {

    public static final String TABLENAME = "t_custody";

    /**
     * Properties of entity Custody.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Name = new Property(1, String.class, "name", false, "NAME");
        public final static Property Head = new Property(2, String.class, "head", false, "HEAD");
        public final static Property Relation = new Property(3, String.class, "relation", false, "RELATION");
        public final static Property Card = new Property(4, String.class, "card", false, "CARD");
    }

    private Query<Custody> baby_CustodysQuery;

    public CustodyDao(DaoConfig config) {
        super(config);
    }
    
    public CustodyDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"t_custody\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"NAME\" TEXT NOT NULL ," + // 1: name
                "\"HEAD\" TEXT," + // 2: head
                "\"RELATION\" TEXT," + // 3: relation
                "\"CARD\" TEXT);"); // 4: card
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"t_custody\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Custody entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getName());
 
        String head = entity.getHead();
        if (head != null) {
            stmt.bindString(3, head);
        }
 
        String relation = entity.getRelation();
        if (relation != null) {
            stmt.bindString(4, relation);
        }
 
        String card = entity.getCard();
        if (card != null) {
            stmt.bindString(5, card);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Custody entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getName());
 
        String head = entity.getHead();
        if (head != null) {
            stmt.bindString(3, head);
        }
 
        String relation = entity.getRelation();
        if (relation != null) {
            stmt.bindString(4, relation);
        }
 
        String card = entity.getCard();
        if (card != null) {
            stmt.bindString(5, card);
        }
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Custody readEntity(Cursor cursor, int offset) {
        Custody entity = new Custody( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getString(offset + 1), // name
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // head
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // relation
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4) // card
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Custody entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setName(cursor.getString(offset + 1));
        entity.setHead(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setRelation(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setCard(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Custody entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Custody entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Custody entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "custodys" to-many relationship of Baby. */
    public List<Custody> _queryBaby_Custodys(Long babyId) {
        synchronized (this) {
            if (baby_CustodysQuery == null) {
                QueryBuilder<Custody> queryBuilder = queryBuilder();
                queryBuilder.join(BabyCustody.class, BabyCustodyDao.Properties.CustodyId)
                    .where(BabyCustodyDao.Properties.BabyId.eq(babyId));
                baby_CustodysQuery = queryBuilder.build();
            }
        }
        Query<Custody> query = baby_CustodysQuery.forCurrentThread();
        query.setParameter(0, babyId);
        return query.list();
    }

}
